﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;

namespace WhiteRepositoryExplorer.Model
{
    public class RecentFiles
    {
        private const string XmlName = "RecentFiles.xml";
        private const string TagRoot = "RecentFiles";
        private const string TagElement = "RecentFile";
        private readonly XmlDocument _xdoc = new XmlDocument();
        private readonly List<RecentFile> _recentFiles = new List<RecentFile>();

        public RecentFiles()
        {
            if (!File.Exists(XmlName))
            {
                XmlDeclaration declaration = _xdoc.CreateXmlDeclaration("1.0", null, null);
                _xdoc.AppendChild(declaration);
                XmlNode root = _xdoc.CreateElement(TagRoot);
                _xdoc.AppendChild(root);
                _xdoc.Save(XmlName);
                return;
            }


        }

        public List<RecentFile> Files { get { return _recentFiles; } }

        public void Add(string path)
        {
            if (_recentFiles.Count >= 5)
            {
                _recentFiles.RemoveAt(_recentFiles.Count -1);
                _recentFiles.Insert(0, new RecentFile(path));
            }

            if (_recentFiles.Any(i => i.FilePath.Equals(path)))
            {
                RecentFile file = _recentFiles.First(i => i.FilePath.Equals(path));
                _recentFiles.Remove(file);
                _recentFiles.Insert(0, file);
            }
            else
            {
                _recentFiles.Insert(0, new RecentFile(path));    
            }
            Save();
            
        }

        private void Generate()
        {
            _xdoc.RemoveAll();

            XmlDeclaration declaration = _xdoc.CreateXmlDeclaration("1.0", null, null);
            _xdoc.AppendChild(declaration);
            XmlNode root = _xdoc.CreateElement(TagRoot);
            
            foreach (RecentFile recentFile in _recentFiles)
            {
                XmlNode node = _xdoc.CreateElement(TagElement);
                node.InnerText = recentFile.FilePath;
                root.AppendChild(node);
            }
            _xdoc.AppendChild(root);
        }

        public void Save()
        {
            Generate();
            _xdoc.Save(XmlName);
        }

        public void LoadXml()
        {
            _recentFiles.Clear();
            _xdoc.Load(XmlName);
            XmlNodeList nodes =_xdoc.GetElementsByTagName(TagElement);
            foreach (XmlNode node in nodes)
            {
                _recentFiles.Add(new RecentFile(node.InnerText));
            }
        }

        public void Remove(string fullPath)
        {
            RecentFile file = _recentFiles.Find(ri => ri.FilePath.Equals(fullPath));
            if (file != null) _recentFiles.Remove(file);
            Save();
        }

        public void Remove(int indexZeroBased)
        {
            _recentFiles.RemoveAt(indexZeroBased);
            Save();
        }
    }
}
